package com.leetcode.partition1;

/**
 * @author `RKC`
 * @date 2021/10/22 12:23
 */
public class LC11盛最多水的容器 {

    public static int maxArea(int[] height) {
        return doublePointer(height);
    }

    public static void main(String[] args) {
        int[] height = {1, 8, 6, 2, 5, 4, 8, 3, 7};
        System.out.println(maxArea(height));
    }

    private static int doublePointer(int[] height) {
        int left = 0, right = height.length - 1, answer = 0;
        while (left < right) {
            //当前位置的盛水量的高是由两块挡板最低处和距离决定的
            int area = Math.min(height[left], height[right]) * (right - left);
            answer = Math.max(answer, area);
            //左边挡板比右边挡板低，就尝试将左边挡板向右移动
            if (height[left] <= height[right]) left++;
            else right--;
        }
        return answer;
    }
}
